﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class storeinfor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        else
        {
            if (!IsPostBack)
            {
                this.bind();
            }
        }
        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员" && JudgePower != "仓库管理员" && JudgePower != "销售" && JudgePower != "主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select WID as 仓库编号,WName as 名称,WArea as 容积,WAddress as 地址,WDescribe as 简单描述 from [Warehouses]";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "仓库编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();
        myConn.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Button2.Visible = true;
        Button3.Visible = true;

        Label1.Visible = false;
        Label2.Visible = false;
        Label3.Visible = false;
        TextBox1.Visible = false;
        Label4.Visible = false;
        TextBox2.Visible = false;
        Label5.Visible = false;
        TextBox3.Visible = false;
        Label6.Visible = false;
        TextBox4.Visible = false;
        Button6.Visible = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;
        TextBox1.Visible = true;
        Label4.Visible = true;
        TextBox2.Visible = true;
        Label5.Visible = true;
        TextBox3.Visible = true;
        Label6.Visible = true;
        TextBox4.Visible = true;
        Button6.Visible = true;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select * from [Warehouses] where WID='" + GridView1.SelectedValue.ToString() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        SqlDataReader read = myCmd.ExecuteReader();
        read.Read();
        Label2.Text = read["WID"].ToString().Trim();
        TextBox1.Text = read["WName"].ToString().Trim();
        TextBox2.Text = read["WArea"].ToString().Trim();
        TextBox3.Text = read["WAddress"].ToString().Trim();
        TextBox4.Text = read["WDescribe"].ToString().Trim();
        read.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/addcangku.aspx?");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "delete from [Warehouses] where WID='" + GridView1.SelectedValue.ToString() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        myConn.Close();
        //Response.Write("<script>alert('删除成功!')</script>");
        Response.Redirect("~/success.aspx?");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.AddHeader("Refresh", "0"); 
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/storedaochu.aspx?");
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "update [Warehouses] set WName='" + TextBox1.Text.Trim() + "',WArea='" + TextBox2.Text.Trim() + "',WAddress='" + TextBox3.Text.Trim() + "',WDescribe='" + TextBox4.Text.Trim() + "' where WID='" + Label2.Text.Trim() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        myConn.Close();
        //Response.Write("<script>alert('修改成功!')</script>");
        Response.Redirect("~/success.aspx?");
    }
}